package com.world.tg.service.impl;

import com.world.tg.dao.UserDao;
import com.world.tg.param.AddressParam;
import com.world.tg.param.RegisterOrLoginParam;
import com.world.tg.pojo.Book;
import com.world.tg.pojo.User;
import com.world.tg.result.BaseResult;
import com.world.tg.result.JsonResult;
import com.world.tg.service.UserService;
import com.world.tg.util.EncryptionUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public BaseResult register(String username,String password) {
        EncryptionUtil encryptionUtil = new EncryptionUtil();
        RegisterOrLoginParam registerParam = new RegisterOrLoginParam();
        Date date = new Date(System.currentTimeMillis());
        Map<String, String> map = encryptionUtil.encryption(password);
        User user = userDao.findUserByName(username);
        BaseResult baseResult = new BaseResult();
        if(user != null){
            baseResult.fail("该用户已存在");
            return baseResult;
        }
        registerParam.set(username,map.get("password"),date,date,map.get("salt"));
        Integer register = userDao.register(registerParam);
        if (register > 0){
            baseResult.success("注册成功");
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            token.setRememberMe(true);
            subject.login(token);
            return baseResult;
        }
        baseResult.fail("注册失败");
        return baseResult;
    }

    @Override
    public BaseResult login(String username, String password) {
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        Subject subject = SecurityUtils.getSubject();
        BaseResult baseResult = new BaseResult();
        token.setRememberMe(true);
        try {
            subject.login(token);
            baseResult.success("登录成功");
            return baseResult;
        }catch (ShiroException e){
            baseResult.fail("用户名或密码不正确");
            return baseResult;
        }
    }

    @Override
    public BaseResult editUser(User user) {
        Subject subject = SecurityUtils.getSubject();
        BaseResult baseResult = new BaseResult();
        Date date = new Date(System.currentTimeMillis());
        user.setUpdated(date);
        if(subject.isAuthenticated()){
            String username = (String) subject.getPrincipal();
            user.setUid(userDao.getUidByUsername(username));
            Integer integer = userDao.editUser(user);
            if (integer > 0){
                baseResult.success("修改成功");
                return baseResult;
            }
            baseResult.fail("修改失败");
            return baseResult;
        }
        baseResult.success("请登录");
        return baseResult;
    }

    @Override
    public JsonResult findUserMsg() {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        JsonResult jsonResult = new JsonResult();
        if(subject.isAuthenticated()){
            User user = userDao.findUserByName(username);
            if (user != null){
                jsonResult.success("success",user);
                return jsonResult;
            }
            jsonResult.fail("fail",null);
            return jsonResult;
        }
        jsonResult.success("fail","请登录");
        return jsonResult;
    }

    @Override
    public BaseResult addAddress(AddressParam param) {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        BaseResult result = new BaseResult();
        Date date = new Date(System.currentTimeMillis());
        if(subject.isAuthenticated()){
            param.setCreated(date);
            param.setUpdated(date);
            Integer integer = userDao.addAddress(param);
            Integer integer1 = userDao.addUserAddress(param.getAid(), userDao.getUidByUsername(username),date,date);
            if(integer > 0 && integer1 > 0){
                result.success("添加成功");
                return result;
            }
            result.fail("添加失败");
            return result;
        }
        result.success("请登录");
        return result;
    }

    @Override
    public JsonResult findAddress() {
        Subject subject = SecurityUtils.getSubject();
        String username = (String)subject.getPrincipal();
        JsonResult jsonResult = new JsonResult();
        if (subject.isAuthenticated()){
            List<AddressParam> address = userDao.findAddressByUid(userDao.getUidByUsername(username));
            jsonResult.success("success",address);
            return jsonResult;
        }
        jsonResult.success("fail","请登录");
        return jsonResult;
    }

    @Override
    public JsonResult findCollect() {
        Subject subject = SecurityUtils.getSubject();
        String username = (String)subject.getPrincipal();
        JsonResult jsonResult = new JsonResult();
        if(subject.isAuthenticated()){
            List<Book> collect = userDao.findCollect(userDao.getUidByUsername(username));
            jsonResult.success("success",collect);
            return jsonResult;
        }
        jsonResult.success("fail","请登录");
        return jsonResult;
    }

    @Override
    public JsonResult editPhoto(String filename) {
        String photo = "/photo/" + filename;
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        JsonResult jsonResult = new JsonResult();
        if (subject.isAuthenticated()){
            Integer integer = userDao.editUserPhoto(photo, userDao.getUidByUsername(username));
            if(integer > 0){
                jsonResult.success("success","修改成功");
                return jsonResult;
            }
            jsonResult.fail("fail","修改失败");
            return jsonResult;
        }
        jsonResult.success("请登录");
        return jsonResult;
    }

    @Override
    public JsonResult getPhoto() {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        JsonResult jsonResult = new JsonResult();
        if(subject.isAuthenticated()){
            String userPhoto = userDao.getUserPhoto(userDao.getUidByUsername(username));
            jsonResult.success("success",userPhoto);
            return jsonResult;
        }
        jsonResult.fail("请登录");
        return jsonResult;
    }

    @Override
    public BaseResult logout() {
        Subject subject = SecurityUtils.getSubject();
        BaseResult baseResult = new BaseResult();
        if (subject.isAuthenticated()){
            subject.logout();
            baseResult.success("退出成功");
        }
        baseResult.success("用户未登录");
        return baseResult;
    }

    @Override
    public JsonResult isLogin() {
        Subject subject = SecurityUtils.getSubject();
        String username = (String)subject.getPrincipal();
        User user = userDao.findUserByName(username);
        JsonResult jsonResult = new JsonResult();
        jsonResult.success("success",user);
        return jsonResult;
    }
}
